<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class C_Users_Edit extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Users_Model');
        $this->load->model('System_Model');
        $this->load->model('Role_Model');
    }

    /*
     * index
     * 初始化
     */

    public function index($id = FALSE) {
        $data['title'] = $this->System_Model->get_system('login_title');
        //判断是否为编辑信息
        $data['entity'] = $this->Users_Model->GetEntity($id);
        $this->load->view('layout/header', $data);
        $this->load->view('layout/menu');
        $this->load->view('users/user_edit_view', $data);
        $this->load->view('layout/footer');
    }

    /*
     * edit_Product
     * 编辑数据库信息
     */

    function edit_User() {
        $pt_id = $this->input->post('u_id');
        if ($this->input->post('u_admin')) {
            $uadmin = 1;
        } else {
            $uadmin = 0;
        }
        $data = array(
            'u_id' => $pt_id,
            'u_name' => $this->input->post('u_name'),
            'u_userName' => $this->input->post('u_userName'),
            'u_password' => $this->input->post('u_password'),
            'u_admin' => $uadmin,
            'u_state' => OBJ_STATE_VALID
        );
        if ($pt_id != NULL || $pt_id != "") {
            $this->Users_Model->UpdateEntity($pt_id, $data);
        } else {
            $number = $this->Users_Model->GetEntityForName($this->input->post('u_name'));
            if ($number > 0) {
                $this->session->set_userdata('alertContent', "用户名已经存在！");
                redirect('users/C_Users_Edit');
            }
            $this->Users_Model->Insert_User($data);
        }
        //跳转
        redirect('users/C_Users_List');
    }

    /* ——————————————————————————————————————————
     * role_index
     * 权限操作
     */

    public function role_index($id = FALSE) {
        $data['title'] = $this->System_Model->get_system('login_title');
        //判断是否为编辑信息
        $data['users'] = $this->Users_Model->GetEntity($id);
        $data['entity'] = $this->Role_Model->GetEntity($id);
        $this->load->view('layout/header', $data);
        $this->load->view('layout/menu');
        $this->load->view('users/user_role_view', $data);
        $this->load->view('layout/footer');
    }

    //添加用户权限
    function role_User() {
        $u_id = $this->input->post('u_id');
        $content = $this->input->post('ro_content');

        //判断是否已经勾选
        if ($content != "") {
            $content = substr($content, 1);
            $data = array(
                'u_id' => $u_id,
                'ro_content' => $content
            );
            $this->Role_Model->Insert_Role($u_id, $data);
        } else {
            $this->Role_Model->Delete($u_id, $data);
        }
        $this->session->set_userdata('alertContent', "提交成功！");
        redirect('users/C_Users_Edit/role_index/' . $u_id);
    }

    /* ——————————————————————————————————————————
     * password_index
     * 修改密码
     */

    function password_index() {
        $data['title'] = $this->System_Model->get_system('login_title');
        //判断是否为编辑信息
        $id = $this->session->userdata('uid');
        $data['users'] = $this->Users_Model->GetEntity($id);
        $this->load->view('layout/header', $data);
        $this->load->view('layout/menu');
        $this->load->view('users/user_password_view', $data);
        $this->load->view('layout/footer');
    }

    //修改密码
    function edit_password() {
        $u_id = $this->input->post('u_id');
        $old_password = $this->input->post('old_password');
        $new_password = $this->input->post('new_password');
        $num = $this->Users_Model->getUserForPassword($u_id, $old_password);
        if ($num > 0) {
            $data = array(
                'u_password' => $new_password
            );
            $this->Users_Model->UpdateEntity($u_id, $data);
            $this->session->set_userdata('alertContent', "修改成功，请记牢自己的新密码！");
            redirect('users/C_Users_Edit/password_index/');
        } else {
            $this->session->set_userdata('alertContent', "原始密码不正确！");
            redirect('users/C_Users_Edit/password_index/');
        }
    }

}

?>
